package pro.shushi.pamirs.demo.core.manager;

import org.springframework.stereotype.Component;
import pro.shushi.pamirs.demo.api.model.DemoItem;
import pro.shushi.pamirs.meta.api.CommonApiFactory;
import pro.shushi.pamirs.meta.api.core.compute.systems.type.gen.IdGenerator;
import pro.shushi.pamirs.meta.api.core.configure.yaml.data.model.PamirsTableInfo;
import pro.shushi.pamirs.meta.common.spi.Spider;
import pro.shushi.pamirs.meta.util.TypeUtils;

/**
 * 示例代码-手动生成ID和Code
 */
@Component
public class ManualCreateIdCodeManager {

    /**
     * 在特定场景下需要手动生成Id或者code时，可参照这个示例
     */
    public void manualSetIdCode(){
        DemoItem demoItem = new DemoItem();
        //手动生成ID和code
        Object idObj =  Spider.getDefaultExtension(IdGenerator.class).generate(PamirsTableInfo.fetchKeyGenerator(DemoItem.MODEL_MODEL));
        demoItem.setId(TypeUtils.createLong(idObj));

        Object codeObj = CommonApiFactory.getSequenceGenerator().generate("SEQ",DemoItem.MODEL_MODEL);
        String code = TypeUtils.stringValueOf(codeObj);
        demoItem.setCode(code);
    }
}
